package com.fuda.trade.ui.orders

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fuda.trade.MyApplication
import com.fuda.trade.ZenohManager
import com.fuda.trade.model.FudaThing
import com.fuda.trade.model.FudaThingId
import com.fuda.trade.model.TradeOrder
import com.fuda.trade.model.TradeOrderData
import com.fuda.trade.ui.strategies.StrategiesViewModel
import io.zenoh.sample.Sample
import kotlinx.coroutines.launch

class OrdersViewModel : ViewModel() {

    companion object {
        val TAG: String = OrdersViewModel::class.java.simpleName
    }

    private val _orders = MutableLiveData<List<TradeOrder>>()
    val orders: LiveData<List<TradeOrder>>
        get() = _orders

    init {
        refreshData()
    }

    private fun updateOrders(orderList: List<TradeOrder>) {
        _orders.postValue(orderList)
    }

    fun refreshData() {
        this.viewModelScope.launch {
            // Fetch the latest data from the server
            fetchLatestOrdersFromServer { newCommands ->
                updateOrders(newCommands)
            }
        }
    }

    private fun fetchLatestOrdersFromServer(onSuccess: (List<TradeOrder>) -> Unit) {
        ZenohManager.ordersSelector?.let {
            MyApplication.zenohManager.select(it,
                object: ZenohManager.ZenohCallback{
                    override fun onSuccess(sample: Sample) {
                        val payload = sample.value.toString()
                        Log.d(TAG, "viewModel got:  $payload" )
                        val orders = TradeOrder.deserializeList(payload)
                        onSuccess(orders)
                    }

                    override fun onError(error: String) {
                        Log.e(StrategiesViewModel.TAG, error)
                        // Simulate fetching data from the server
                        val orders = listOf(
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy(securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 500.00, volume = 500, isAuto = true, desc="New command added")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy(securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 200.00, volume = 200,isAuto = true, desc="desc buy manual2")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Sell(securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 300.00, volume = 300,isAuto = true, desc="desc sell auto3")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Sell(securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 400.00, volume = 400,isAuto = true, desc="desc sell manual4")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 100.00, volume = 100,isAuto = true, desc="desc buy auto1")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 200.00, volume = 200,isAuto = true, desc="desc buy auto2")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 300.00, volume = 300,isAuto = true, desc="desc buy auto3")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 400.00, volume = 400,isAuto = true, desc="desc buy auto4")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 100.00, volume = 100,isAuto = true, desc="desc buy auto1")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 200.00, volume = 200,isAuto = true, desc="desc buy auto2")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 300.00, volume = 300,isAuto = true, desc="desc buy auto3")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 400.00, volume = 400,isAuto = true, desc="desc buy auto4")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 100.00, volume = 100,isAuto = true, desc="desc buy auto1")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 200.00, volume = 200,isAuto = true, desc="desc buy auto2")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 300.00, volume = 300,isAuto = true, desc="desc buy auto3")),
                            TradeOrder(
                                id=FudaThing("trade_order",FudaThingId("order001")),
                                data= TradeOrderData.Buy( securityCode = "SH.STK.600519", securityName = "SH.STK.600519", pricingType = TradeOrderData.PricingType.Limit, price = 400.00, volume = 400,isAuto = true, desc="desc buy auto4")),
                        )
                        onSuccess(orders)
                    }
                }
            )
        }
    }
}